//
//  HOPRefreshController.swift
//  WB01
//
//  Created by HOP on 15/12/23.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit
// 自定义刷新控件.里面会包含一个自定的view
class HOPRefreshController: UIRefreshControl {
    private let RefreshControlOffsetY: CGFloat = -60
    private var isUp = false
    // 覆盖父类的属性,监听frame的改变
  override  var frame: CGRect {
        didSet {
//        print("frame: \(frame)")
        // 1.origin.y大于0,看不到刷新控件
            if frame.origin.y > 0 {
            return
            }
            if refreshing {
            print("正在刷新数据")
            refreshView.startLoading()
            }
            // 小于 -60, 箭头转上去
            if frame.origin.y < RefreshControlOffsetY && !isUp {
            isUp = !isUp
//            print("箭头转上去动画")
            refreshView.rotationTipIcon(isUp)
            }else if frame.origin.y > RefreshControlOffsetY && isUp{
                isUp = !isUp
//            print("箭头转下来动画")
            refreshView.rotationTipIcon(isUp) 
            }
         }
     }
    /// 重写父类的方法.自己来做额外的事情
    override func endRefreshing() {
        super.endRefreshing()
        print("结束刷新")
        refreshView.endLoading()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init() {
        super.init()
        prepareUI()
    }
 // MARK: - 准备UI
    private func prepareUI() {
        // 添加控件
        // view从xib里面加载出来的时候,已经有大小,大小是在xib里面设置的大小
    addSubview(refreshView)
        // 添加约束
        refreshView.ff_AlignVertical(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
     }
    private lazy var refreshView: HOPRefreshView = HOPRefreshView.refreshView()
}
class HOPRefreshView: UIView  {
    // MARK: - 属性
    /// 箭头
    @IBOutlet weak var tipIcon: UIImageView!
    // 从xib把view加载出来
    @IBOutlet weak var juhuaIcon: UIImageView!
    @IBOutlet weak var tipView: HOPRefreshView!
    class func refreshView() -> HOPRefreshView {
        // xib的路径
        let view = NSBundle.mainBundle().loadNibNamed("RefreshView", owner: nil, options: nil).last as! HOPRefreshView
        return view
    }
    // MARK: - 箭头旋转
    /**
    箭头旋转
    
    - parameter isUp: isUp = true,箭头转上去, isUp = false,箭头转下来
    */
    func rotationTipIcon(isUp: Bool) {
    UIView.animateWithDuration(0.25) { () -> Void in
        self.tipIcon.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
      }
    // MARK: - 菊花旋转
    /// 菊花旋转
    func startLoading() {
    let animKey = "animKey"
        if juhuaIcon.layer.animationForKey(animKey) != nil {
        print("动画正在执行,什么都不干")
        return
        }
        tipView.hidden = true
        // 3.旋转菊花
        // 3.1创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 3.2设置核心动画参数
        anim.duration = 0.25
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        juhuaIcon.layer.addAnimation(anim, forKey: "animKey")
    }
    // MARK: - 结束加载
    /// 结束加载
    func endLoading() {
    tipView.hidden = false
        juhuaIcon.layer.removeAllAnimations()
     }

}


























